<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     个人中心.
 * @Author          Evelyn丶好久不见
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2019/4/28
 */
namespace app\api\controller;
use app\common\controller\Base;
use think\Db;
class Goods extends Base{
    public function goodslist(){
        $where = [];
        $title = input('title','','trim');
        $type = input('type',0,'intval');
        
        if($title){
            // if(db('search')->where(['content'=>$title,'user_id'=>UID])->find()){
            //     db('search')->where(['content'=>$title,'user_id'=>UID])->update(['add_time'=>time()]);
            // }else{
            //     db('search')->insert(['content'=>$title,'user_id'=>UID,'add_time'=>time()]);
            // }
            $where['title']=['like',"%".$title."%"];
        }

        if($type!=0){
            $where['goods_type'] = ['eq',$type];
        }

        $hot = input('hot',0,'intval');
        if($hot == 1){
            $where['hot'] = ['eq',$hot];
        }

        $order = input('order',1,'intval');
        switch ($order) {
            case 1:$order = 'sort desc';break;
            case 2:$order = 'sale_num desc,sort desc';break;
            case 3:$order = 'add_time desc,sort desc';break;
            case 4:$order = 'price desc,sort desc';break;
            case 5:$order = 'price asc,sort desc';break;
            default:break;
        }
        $list = db('goods')->field('id,title,price,price1,banner as image,sale_num')->where($where)->limit(($this->p-1)*10,10)->order($order)->select();
        foreach ($list as $key => $value) {
            $list[$key]['lable'] = db('goods_lable')->field('lable')->where('goods_id',$value['id'])->select();
            $list[$key]['image'] = $this->host.explode(',',$value['image'])[0];
            // $list[$key]['car_num'] = db('shop_car')->where(['user_id'=>UID,'goods_id'=>$value['id']])->sum('num');
        }
        $this->apireturn(1,'商品列表',$list);
    }

//商品详情
    public function goodsinfo(){
        $id = input('id',0,'intval');
        $info = db('goods')->field('id,title,price,price1,banner,content')->where(['id'=>$id])->find();
        if(!$info)
            $this->apireturn(0,'商品信息错误');
        $info['type'] = db('goods_type')->where(['goods_id'=>$info['id']])->select();
        $banner=explode(',',$info['banner']);
        foreach ($banner as $key => $value) {
            $banner[$key] = $this->host.$value;
        }

        $evaluate = db('evaluate')->alias('e')->join('user u','u.id = e.user_id','left')->field('e.id,stars,content,nickname,photo_path,image')->where(['goods_id'=>$id])->order('add_time desc')->limit(0,2)->select();
        //echo db('evaluate')->getlastsql();die;
        
        foreach ($evaluate as $key => $value) {
            $evaluate[$key]['image'] = explode(',',$value['image']);
        }
        
        $info['banner'] = $banner;
        $info['evaluate'] = $evaluate;
        $info['collection'] = 0;
        if(input('token','','trim')!=''){

            $Aes = new \Aes\Aes();
            $token = $Aes->aes128cbcHexDecrypt(input('token','','trim'));
            $userinfo = db('user')->where('token',$token)->find();
            if($userinfo){
                if(db('collection_goods')->where(['user_id'=>$userinfo['id'],'goods_id'=>$id])->find()){
                    $info['collection'] = 1;
                }
            }
        }

        
        $this->apireturn(1,'商品信息',$info);
    }
}